package com.homework.hw1028;

import java.util.Scanner;

public class Rule {
    Person person = new Person();
    Computer computer = new Computer();
    int a = person.getScore();
    int b = person.getScore();

    public void ready(String pname, int pchoice, Person person, Computer computer) {
        if (pchoice == 1) {
            computer.setName("刘备");
        } else if (pchoice == 2) {
            computer.setName("孙权");
        } else if (pchoice == 3) {
            computer.setName("曹操");
        }
        person.setName(pname);
        System.out.println(person.getName() + " VS " + computer.getName() + "  对战");
    }

    public int whoWin(String ppunch, String cpunch) {
        if (ppunch.equals(cpunch)) {
            return 0; //平局
        } else if (ppunch.equals("剪刀")) {
            if (cpunch.equals("石头")) {
                return 1; // 电脑赢
            } else {
                return 2; // 人赢
            }
        } else if (ppunch.equals("石头")) {
            if (cpunch.equals("布")) {
                return 1;
            } else {
                return 2;
            }
        } else if (ppunch.equals("布")) {
            if (cpunch.equals("剪刀")) {
                return 1;
            } else {
                return 2;
            }
        }
        return 0;
    }

    public void scoreChange(int result, Person person, Computer computer) {
        if (result == 1) {
            a++;
            computer.setScore(a);
        } else if (result == 2) {
            b++;
            person.setScore(b);
        } else {
            a += 0;
            b += 0;
        }
    }

    public void gameResult(String perresult, String comresult, Rule rule, Person person, Computer computer) {
        int resultNum = rule.whoWin(perresult, comresult);
        if (resultNum == 1) {
            System.out.println("哈哈.你输啦!");
        } else if (resultNum == 2) {
            System.out.println("恭喜,你赢了!");
        } else {
            System.out.println("平局");
        }
        rule.scoreChange(resultNum, person, computer);
    }

    public int gameScore(Person person, Computer computer) {
        System.out.println("姓名      得分");
        System.out.println(person.getName() + "      " + person.getScore());
        System.out.println(computer.getName() + "      " + computer.getScore());
        if (computer.getScore() > person.getScore()) {
            return 1;
        } else if (computer.getScore() < person.getScore()) {
            return 2;
        } else {
            return 0;
        }
    }

    public String gameend(int a) {
        if (a == 1) {
            return "结果:再接再厉!";
        } else if (a == 2) {
            return "结果:恭喜恭喜!";
        } else {
            return "结果:势均力敌!";
        }
    }

    public  void ifStart() {
        Scanner input = new Scanner(System.in);
        System.out.println("要开始吗?(y/n)");
        while (true) {
            String startAnswer = input.next();
            if (startAnswer.equals("y")) {
                break;
            } else if (startAnswer.equals("n")) {
                System.out.println("退出游戏");
                System.exit(0);
            } else {
                System.out.println("输入非法字符,请重新输入");
            }
        }
    }

    public boolean ifContinue() {
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.println("是否开启下一轮?(y/n)");
            String ifContinue = input.next();
            if (ifContinue.equals("y")) {
                return true;
            } else if (ifContinue.equals("n")) {
                return false;
            } else {
                System.out.println("输入非法字符,请重新输入");
            }
        }
    }


}

